<?php
/**
 * Created by PhpStorm.
 * User: Dick
 * Date: 2016/3/23
 * Time: 16:20
 */

namespace Controller;

use Model\User;
use Method\Validata;
use Method\Response;
class Member
{
    public static function Login()
    {
        $username = $_POST['username'];
        $pwd = $_POST['pwd'];
        if ($username && $pwd) {
            if ($isEmail = Validata::isEmail($username)) {
                $user = User::GetUserByEmail($username);
                if (!$user) {
                    echo '没有该用户';
                    Response::redirect('index');
                } elseif (!$user->verifyPassword($pwd)) {
                    echo '密码错误';
                    Response::redirect('index');
                } else {
                    echo 'success!';
                    $_SESSION['user'] = $user;
                    Response::redirect('HomePage');
//                    if (!empty($_POST['remember_me'])) {
//                        setcookie("user", $username, time() + 3600 * 24 * 365);
//                        setcookie("user", $pwd, time() + 3600 * 24 * 365);
//                    }else{
//                        setcookie("user",null,time()-1);
//                        setcookie("pwd",null,time()-1);
//                    }todo:setcookie
                }
            } else {
                $user = User::GetUserByUsername($username);
                if (!$user) {
                    echo '没有该用户';
                    Response::redirect('index');
                } elseif (!$user->verifyPassword($pwd)) {
                    echo '密码错误';
                    Response::redirect('index');
                } else {
                    echo 'success!';
                    $_SESSION['user'] = $user;
                    Response::redirect('HomePage');
//                    if (!empty($_POST['remember_me'])) {
//                        setcookie("user", $username, time() + 3600 * 24 * 365);
//                        setcookie("user", $pwd, time() + 3600 * 24 * 365);
//                    }else{
//                        setcookie("user",null,time()-1);
//                        setcookie("pwd",null,time()-1);
//                    }
                }
            }
        } else {
            Response::redirect('index');
        }
    }

    public static function Register()
    {
        if ($_POST['username'] && $_POST['email'] && $_POST['password']) {
            if (User::GetUserByEmail($_POST['email'])) {
                echo '该邮箱已经注册';
                Response::redirect('index');
            }

            $user = new User();
            $user->username = $_POST['username'];
            $user->email = $_POST['email'];
            $user->registtime = date("Y-m-d H:i:s");
            $user->insertToDB();
            $user->savePassword($_POST['password']);

            echo 'Registration complete. ';
            $_SESSION['user'] = $user;
            sleep(1);
            Response::redirect('HomePage');
        } else {
            Response::redirect('index');
        }
    }

    public function logout(){
        unset($_SESSION['user']);
        sleep(1);
        Response::redirect('index');
    }
}